/*
 * WriteLn.cpp
 *
 *  Created on: 17.04.2012
 *      Author: Eugene Kornykhin
 */

#include "Writer.h"
#include "Number.h"
#include "String.h"
#include "../Interpreter.h"
#include <stdexcept>
#include <iostream>
#include <typeinfo>


void WriteInt::write(Interpreter &interpreter) const
{
	const PElement &element = interpreter.popPolizElement();
	try {
		const Number &value = dynamic_cast<const Number &>(element);
		std::cout << value.getValue();
	} catch(std::bad_cast&) {
		throw std::logic_error("WriteInt excepts a number value");
	}
}

Writer *WriteInt::clone() const
{
	return new WriteInt();
}


string WriteInt::toString() const
{
	return "WRITE_INTEGER";
}

void WriteLn::write(Interpreter &interpreter) const
{
	std::cout << std::endl;
}

Writer *WriteLn::clone() const
{
	return new WriteLn();
}

string WriteLn::toString() const
{
	return "WRITELN";
}

void WriteString::write(Interpreter &interpreter) const
{
	const PElement &element = interpreter.popPolizElement();
	try {
		const String &value = dynamic_cast<const String &>(element);
		std::cout << value.getValue();
	} catch(std::bad_cast&) {
		throw std::logic_error("WriteString excepts a string value");
	}
}

Writer *WriteString::clone() const
{
	return new WriteString();
}

string WriteString::toString() const
{
	return "WRITE_STRING";
}

